package me.heshang.rednamedrop.Connection;

import me.heshang.rednamedrop.Drop;
import me.heshang.rednamedrop.PlayerkilTitle;
import me.heshang.rednamedrop.RedWorld;
import org.bukkit.Bukkit;

import java.sql.*;

public class MySql {
    private static String ip;//ip地址
    private static String port;//端口
    private static String databasename;//数据库数据名
    private static String username;//用户名
    private static String passworld;//密码
    private static String tabName;//表名
    private static String url;
    static Connection connection = null;
    static ResultSet resultSet = null;
    static Statement st = null;
    static PreparedStatement ps = null;
    private static int LEVER;
    private static int KillCreature;

    public static void getShut() throws SQLException {
        connection.close();
    }
    /**
     * 查询
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void join(String name) throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        String sql = "SELECT * FROM  "+MySql.getTabName();
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        while (rs.next()){
            if(rs.getString("name").equals(name)){//判断玩家是否存在数据库中
                connection.close();
                return;
            }
        }
        sql = "INSERT INTO "+MySql.getTabName()+" (name,lever,killcreature) values (?,?,?)";//向hs_rednamedrop表里添加数据
        ps  = (PreparedStatement) connection.prepareStatement(sql);
        ps.setString(1, name);
        ps.setInt(2,0);
        ps.setInt(3,0);
        ps.executeUpdate();
        ps.close();
        connection.close();
        Bukkit.getConsoleSender().sendMessage("§b[RedNamedrop]> §e玩家:"+name+"不存在将进行添加");
    }
    public void insert() throws SQLException {
        if (!new Drop().NAME.equals(new Drop().getP())){
            try {
                String sql = "INSERT INTO "+MySql.getTabName()+" (name,lever) values (?,?)";//向hs_rednamedrop表里添加数据
                ps  = (PreparedStatement) connection.prepareStatement(sql);
                ps.setString(1, new Drop().getP());
                ps.setInt(2,0);
                ps.executeUpdate();
                //关闭数据库操作
                ps.close();
                connection.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }
    /**
     * 数据库操作
     * 更新数据
     * 玩家每杀一人lever则增加1
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void change() throws SQLException, ClassNotFoundException {
        new MySql().getSql();

        String sql = "SELECT * FROM  "+MySql.getTabName();
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        int id = 0;
        while (rs.next()){
            try {
            if(rs.getString("name").equals(new PlayerkilTitle().getPl())){//判断玩家是否存在数据库中
                id = rs.getInt("ID");
                int lever = rs.getInt("lever")+1;
                sql = "update hs_rednamedrop set lever=? where ID=?";
                ps = connection.prepareStatement(sql);
                ps.setDouble(1,lever);
                ps.setInt(2,id);

                int n=ps.executeUpdate();
                //System.out.println(n>0?"更新成功":"失败");
                LEVER = rs.getInt("lever");
                ps.close();
                connection.close();
                return;
            }
        } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void killcreature() throws SQLException, ClassNotFoundException {
        new MySql().getSql();

        String sql = "SELECT * FROM  "+MySql.getTabName();
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        int id = 0;
        while (rs.next()){
            try {
                if(rs.getString("name").equals(new PlayerkilTitle().getPl())){//判断玩家是否存在数据库中
                    id = rs.getInt("ID");
                    int killcreature = rs.getInt("killcreature")+1;
                    sql = "update hs_rednamedrop set killcreature=? where ID=?";
                    ps = connection.prepareStatement(sql);
                    ps.setDouble(1,killcreature);
                    ps.setInt(2,id);
                    int n=ps.executeUpdate();
                  //  System.out.println(n>0?"更新成功":"失败");
                    KillCreature = rs.getInt("killcreature");
                    ps.close();
                    connection.close();
                    return;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public void emptykillcreature() throws SQLException, ClassNotFoundException {
        new MySql().getSql();

        String sql = "SELECT * FROM  "+MySql.getTabName();
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        int id = 0;
        while (rs.next()){
            try {
                if(rs.getString("name").equals(new PlayerkilTitle().getPl())){//判断玩家是否存在数据库中
                    id = rs.getInt("ID");
                    sql = "update hs_rednamedrop set killcreature=? where ID=?";
                    ps = connection.prepareStatement(sql);
                    ps.setDouble(1,0);
                    ps.setInt(2,id);
                    int n=ps.executeUpdate();
                    KillCreature = rs.getInt("killcreature");
                    ps.close();
                    connection.close();
                    return;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    /**
     * 数据库操作
     * 更新数据-减少
     * 在红名世界待够指定世界则减"1"
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void reduce() throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        String sql = "SELECT * FROM  "+MySql.getTabName();
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        int id = 0;
        while (rs.next()){
            try {
                if(rs.getString("name").equals(new RedWorld().getPL())){//判断玩家是否存在数据库中
                    if (rs.getInt("lever") >= 1){
                        id = rs.getInt("ID");
                        int lever = rs.getInt("lever")-1;
                        sql = "update "+MySql.getTabName()+" set lever=? where ID=?";
                        ps = connection.prepareStatement(sql);
                        ps.setInt(1,lever);
                        ps.setInt(2,id);
                        int n=ps.executeUpdate();
                        //System.out.println(n>0?"更新成功":"失败");
                        LEVER = rs.getInt("lever");
                        ps.close();
                        connection.close();
                        return;
                    }
                    new RedWorld().thread();
                }
            } catch (SQLException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public void querylever() throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        String sql = "SELECT * FROM  " + MySql.getTabName();
        ResultSet rs = new MySql().getSt().executeQuery(sql);//数据库操作
        while (rs.next()) {
                if (rs.getString("name").equals(new RedWorld().getPL())) {//判断玩家是否存在数据库中
                    LEVER = rs.getInt("lever");
                    KillCreature = rs.getInt("killcreature");
                }
        }
        connection.close();
    }

    /**
     * 数据库连接操作
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public void getSql() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        MySql.setConnection(DriverManager.getConnection(MySql.getUrl(), MySql.getUsername(), MySql.getPassworld()));
        MySql.setSt(MySql.getConnection().createStatement());
    }

    public static String getIp() {
        return ip;
    }

    public static void setIp(String ip) {
        MySql.ip = ip;
    }

    public static String getPort() {
        return port;
    }

    public static void setPort(String port) {
        MySql.port = port;
    }

    public static String getDatabasename() {
        return databasename;
    }

    public static void setDatabasename(String databasename) {
        MySql.databasename = databasename;
    }

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        MySql.username = username;
    }

    public static String getPassworld() {
        return passworld;
    }

    public static void setPassworld(String passworld) {
        MySql.passworld = passworld;
    }

    public static String getTabName() {
        return tabName;
    }

    public static void setTabName(String tabName) {
        MySql.tabName = tabName;
    }

    public static void setConnection(Connection connection) {
        MySql.connection = connection;
    }

    public static ResultSet getResultSet() {
        return resultSet;
    }

    public static void setResultSet(ResultSet resultSet) {
        MySql.resultSet = resultSet;
    }

    public static Statement getSt() {
        return st;
    }

    public static void setSt(Statement st) {
        MySql.st = st;
    }

    public static Connection getConnection() {
        return connection;
    }
    public static PreparedStatement getPs() {
        return ps;
    }
    public static void setPs(PreparedStatement ps) {
        MySql.ps = ps;
    }
    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        MySql.url = url;
    }

    public static int getLEVER() {
        return LEVER;
    }

    public static void setLEVER(int LEVER) {
        MySql.LEVER = LEVER;
    }

    public static int getKillCreature() {
        return KillCreature;
    }

    public static void setKillCreature(int killCreature) {
        KillCreature = killCreature;
    }

}
